package org.piaohao.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.piaohao.model.Vehicle;
import org.piaohao.util.string.JsonUtil;

/**
 * Created on : 2013-5-4, 17:24:11
 *
 * @author piaohao
 */
public class StatisticController extends Controller {

    public static final String moduleName = "统计分析模块";

    public void driver() {
        try {
            render("/pages/statistic/driver.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getDriverByOrg() {
        try {
            List<Record> records = Db.find("select o.name,count(*) as total from t_driver d left join t_organization o on "
                    + " o.id=d.org_id group by d.org_id");
            renderJson(JsonUtil.printGrid(0L, records));
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }

    public void getDriverByAge() {
        try {
            List<Record> records = Db.find("select name, count(*) as total from ("
                    + "select "
                    + "case "
                    + "when age>=20 and age<30 then '20-29' "
                    + "when age>=30 and age<40 then '30-39' "
                    + "when age>=40 and age<50 then '40-49' "
                    + "when age>=50 and age<60 then '50-59' "
                    + "when age>=60 and age<=70 then '60-69' "
                    + "end "
                    + "as name from t_driver"
                    + ") t_driver group by name");

            renderJson(JsonUtil.printGrid(0L, records));
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }

    public void getDriverByEdu() {
        try {
            List<Record> records = Db.find("select d.education as name,count(*) as total from t_driver d group by d.education");
            renderJson(JsonUtil.printGrid(0L, records));
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }

    public void getDriverBySex() {
        try {
            List<Record> records = Db.find("select d.sex as name,count(*) as total from t_driver d group by d.sex");
            renderJson(JsonUtil.printGrid(0L, records));
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }

    public void vehicle() {
        try {
            render("/pages/statistic/vehicle.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getVehicleByOrg() {
        try {
            List<Record> records = Db.find("select o.name,count(*) as total from t_vehicle v left join t_organization o on "
                    + " o.id=v.org_id group by v.org_id");
            renderJson(JsonUtil.printGrid(0L, records));
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }

    public void getVehicleByType() {
        try {
            List<Record> records = Db.find("select v.type as name,count(*) as total from t_vehicle v  group by v.type");
            renderJson(JsonUtil.printGrid(0L, records));
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }

    public void getVehicleByColor() {
        try {
            List<Record> records = Db.find("select v.veh_color as name,count(*) as total from t_vehicle v group by v.veh_color");
            renderJson(JsonUtil.printGrid(0L, records));
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }

    public void getVehicleByPlate() {
        try {
            List<Vehicle> vehs = Vehicle.dao.find("select * from t_vehicle");
            ArrayList<HashMap<String, Object>> records = new ArrayList<HashMap<String, Object>>();
            Map<String, Long> map = new HashMap<String, Long>();
            for (int i = 0; i < vehs.size(); i++) {
                Vehicle veh = vehs.get(i);
                String number = veh.getStr("plate_number");
                switch (number.charAt(1)) {
                    case 'A':
                        if (map.get("A") == null) {
                            map.put("A", 0L);
                        }
                        map.put("A", map.get("A") + 1);
                        break;
                    case 'B':
                        if (map.get("B") == null) {
                            map.put("B", 0L);
                        }
                        map.put("B", map.get("B") + 1);
                        break;
                    case 'C':
                        if (map.get("C") == null) {
                            map.put("C", 0L);
                        }
                        map.put("C", map.get("C") + 1);
                        break;
                    case 'D':
                        if (map.get("D") == null) {
                            map.put("D", 0L);
                        }
                        map.put("D", map.get("D") + 1);
                        break;
                    case 'E':
                        if (map.get("E") == null) {
                            map.put("E", 0L);
                        }
                        map.put("E", map.get("E") + 1);
                        break;
                    case 'F':
                        if (map.get("F") == null) {
                            map.put("F", 0L);
                        }
                        map.put("G", map.get("G") + 1);
                        break;
                    case 'G':
                        if (map.get("G") == null) {
                            map.put("G", 0L);
                        }
                        map.put("G", map.get("G") + 1);
                        break;
                    case 'H':
                        if (map.get("H") == null) {
                            map.put("H", 0L);
                        }
                        map.put("H", map.get("H") + 1);
                        break;
                    case 'I':
                        if (map.get("I") == null) {
                            map.put("I", 0L);
                        }
                        map.put("I", map.get("I") + 1);
                        break;
                    case 'J':
                        if (map.get("J") == null) {
                            map.put("J", 0L);
                        }
                        map.put("J", map.get("J") + 1);
                        break;
                    case 'K':
                        if (map.get("K") == null) {
                            map.put("K", 0L);
                        }
                        map.put("K", map.get("K") + 1);
                        break;
                    case 'L':
                        if (map.get("L") == null) {
                            map.put("L", 0L);
                        }
                        map.put("L", map.get("L") + 1);
                        break;
                    case 'M':
                        if (map.get("M") == null) {
                            map.put("M", 0L);
                        }
                        map.put("M", map.get("M") + 1);
                        break;
                    case 'O':
                        if (map.get("O") == null) {
                            map.put("O", 0L);
                        }
                        map.put("O", map.get("O") + 1);
                        break;
                    case 'P':
                        if (map.get("P") == null) {
                            map.put("P", 0L);
                        }
                        map.put("P", map.get("P") + 1);
                        break;
                    case 'Q':
                        if (map.get("Q") == null) {
                            map.put("Q", 0L);
                        }
                        map.put("Q", map.get("Q") + 1);
                        break;
                    case 'R':
                        if (map.get("R") == null) {
                            map.put("R", 0L);
                        }
                        map.put("R", map.get("R") + 1);
                        break;
                    case 'S':
                        if (map.get("S") == null) {
                            map.put("S", 0L);
                        }
                        map.put("S", map.get("S") + 1);
                        break;
                    case 'T':
                        if (map.get("T") == null) {
                            map.put("T", 0L);
                        }
                        map.put("T", map.get("T") + 1);
                        break;
                    case 'U':
                        if (map.get("U") == null) {
                            map.put("U", 0L);
                        }
                        map.put("U", map.get("U") + 1);
                        break;
                    case 'V':
                        if (map.get("V") == null) {
                            map.put("V", 0L);
                        }
                        map.put("V", map.get("V") + 1);
                        break;
                    case 'W':
                        if (map.get("W") == null) {
                            map.put("W", 0L);
                        }
                        map.put("W", map.get("W") + 1);
                        break;
                    case 'X':
                        if (map.get("X") == null) {
                            map.put("X", 0L);
                        }
                        map.put("X", map.get("X") + 1);
                        break;
                    case 'Y':
                        if (map.get("Y") == null) {
                            map.put("Y", 0L);
                        }
                        map.put("Y", map.get("Y") + 1);
                        break;
                    case 'Z':
                        if (map.get("Z") == null) {
                            map.put("Z", 0L);
                        }
                        map.put("Z", map.get("Z") + 1);
                        break;

                }
            }
            for (Entry<String, Long> entry : map.entrySet()) {
                HashMap<String, Object> tmp = new HashMap<String, Object>();
                tmp.put("name", entry.getKey());
                tmp.put("total", entry.getValue());
                records.add(tmp);
            }
            renderJson(JsonUtil.printGrid(0L, records));
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }
}
